﻿using System;
using System.Windows.Input;
using Samurai.DAL;
using Samurai.Infrastructure;
using Samurai.MovieIndustry;
using System.ComponentModel;
using System.Threading;
using System.Net;
using System.Windows.Threading;

namespace Samurai.Client.Commands
{
    public class CreateMovieCommand : ICommand
    {

        private static CreateMovieCommand _instance;
        public static CreateMovieCommand Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new CreateMovieCommand();
                return _instance;
            }
        }


        private CreateMovieCommand()
        {

        }

        void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var candidateMovie = (CandidateMovie)e.Argument;

                if (!Manager.Instance.Exists(candidateMovie.Path))
                {
                    var movieData = new MovieDirector().Generate(candidateMovie);
                    
                    if (movieData.Error != null && movieData.Error == Constants.ErrorMovieCreation)
                    {
                        _isError = true;
                    }

                    else
                    {

                        Manager.Instance.AddMovie(movieData);
                    }
                }
            }
            catch (Exception ex)
            {
                FailedWorking(ex);
            }
        }

        void BackgroundWorkerRunWorkerCompleted(
            object sender,
            RunWorkerCompletedEventArgs e)
        {
            InvokeMovieCreated(new EventArgs());
        }

        public event EventHandler MovieCreated;

        public void InvokeMovieCreated(EventArgs e)
        {
            var handler = MovieCreated;
            if (handler != null) handler(this, e);
        }

        private bool _isError;

        public void Execute(object parameter)
        {
            _dispatcher = Dispatcher.CurrentDispatcher;

            var candidateMovies = Manager.Instance.GetCandidateMovies();
            do
            {
                _isError = false;

                foreach (var candidateMovie in candidateMovies)
                {
                    var backgroundWorker = new BackgroundWorker();

                    backgroundWorker.DoWork += BackgroundWorkerDoWork;

                    backgroundWorker.RunWorkerCompleted += BackgroundWorkerRunWorkerCompleted;

                    backgroundWorker.RunWorkerAsync(candidateMovie);
                }
            } while (_isError);

        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;


        private Dispatcher _dispatcher;
        private void FailedWorking(Exception ex)
        {
            _dispatcher.BeginInvoke(new NotifyWorkerFailedDelegate(NotifyWorkerFailed), DispatcherPriority.Normal, ex);
        }


        public delegate void NotifyWorkerFailedDelegate(Exception ex);

        private void NotifyWorkerFailed(Exception ex)
        {
            throw ex;
        }
    }

}
